﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace MagicTraderLib.Schema
{
    public class Verzameling
    {
        public int LidNr { get; private set; }
        public List<Kaart> Kaarten { get; private set; }

        public Verzameling(int lidNummer)
        {
            LidNr = lidNummer;
            LeesCollectieVanDisk();
        }

        private void LeesCollectieVanDisk()
        {
            Kaarten = new List<Kaart>();

            var directoryInfo = new DirectoryInfo(String.Format(@"C:\Temp\{0}", LidNr));
            if (!directoryInfo.Exists) 
                return;
            
            foreach (var file in directoryInfo.GetFiles())
            {
                var doc = new HtmlDocument();
                doc.Load(file.FullName, Encoding.GetEncoding(1252));
                
                foreach(HtmlNode tr in doc.DocumentNode.SelectNodes("//tr[@class=\"searchrow\"]"))
                {
                    var tds =
                        tr.ChildNodes.Where(i => i.Name.Equals("td", StringComparison.InvariantCultureIgnoreCase))
                          .ToList();
                    
                    if (tds.Count() != 10)
                        continue;
                    
                    var kaart = new Kaart
                    {
                        AantalWants = ParseInt(tds[0].InnerText)
                        ,AantalCollectie = ParseInt(tds[1].InnerText)
                        ,AantalHaves = ParseInt(tds[2].InnerText)
                        ,Naam = tds[3].InnerText
                        ,Set = tds[4].InnerText
                        ,Kleur = tds[5].InnerText
                        ,Rarety = tds[6].InnerText
                        ,Prijs = ParsePrijs(tds[8].InnerText)
                        , Foil = IsFoil(tds[7]) // add to cart
                    };
                    
                    if (kaart.TotaalAantal != 0 )
                        this.Kaarten.Add(kaart);
                }
            }
        }

        private static bool IsFoil(HtmlNode node)
        {
            // <div class="kaartitemfoil"
            bool isFoil = node.ChildNodes
                .Where(n => n.Name.Equals("div"))
                .SelectMany(i => i.Attributes.AttributesWithName("class"))
                .Any(i => i.Value.Equals("kaartitemfoil"));

            return isFoil;
        }

        private static decimal ParsePrijs(string innerText)
        {
            if (innerText == null || String.IsNullOrWhiteSpace(innerText))
                return 0M;

            innerText = innerText.Replace("&euro;", "").Trim();
            decimal result;

            decimal.TryParse(innerText, out result);

            return result;
        }

        private static int ParseInt(string innerText)
        {
            if (innerText == null || String.IsNullOrWhiteSpace(innerText))
                return 0;
            int result;

            int.TryParse(innerText, out result);
            return result;
        }
    }
}
